package bag

import (
	"go_server/game_server/api/service"
	"go_server/game_server/dao/bag_dao"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/dataconfig/configpb"
	"go_server/game_server/event_manager"
	"go_server/game_server/event_manager/event_data"
	"go_server/game_server/event_manager/event_data/event_impl"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/game_server/world_manager"
	"go_server/zinx/zconstant"
	"go_server/zinx/zlog"
	"go_server/zinx/zutils"
)

type bagService struct {
	service.AbsService
	roleBagInfo  *zutils.ConcurrentHashMap[int64]
	eventMethods map[event_data.EventType]func(event event_data.IEvent)
}

var (
	serviceObj *bagService
)

func init() {
	serviceObj = &bagService{
		roleBagInfo:  zutils.NewConcurrentHashMap[int64](zconstant.CONCURRENT_SEG, zconstant.CONCURRENT_TOTAL),
		eventMethods: make(map[event_data.EventType]func(event event_data.IEvent)),
	}
	event_manager.EventManagerObj.AddServiceEvent(event_data.LoginEvent, onBagLoginEvent, serviceObj)
	event_manager.EventManagerObj.AddServiceEvent(event_data.ServerShutdownEvent, onServerShutdownEvent, serviceObj)
}

func onServerShutdownEvent(event event_data.IEvent) {
	serverShutdownEvent := event.(*event_impl.ServerShutdownEvent)
	roleId := serverShutdownEvent.RoleId
	defer func() {
		serviceObj.roleBagInfo.Remove(roleId)
		serverShutdownEvent.Wg.Done()
	}()
	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo != nil {
		bag_dao.NewOrUpdateBagInfo(bagInfo[pb.ProtocolManager_GetBagList],
			bagInfo[pb.ProtocolManager_GetCurrencyList], roleId)
	}
}

func OnRoleLogoutCallBack(roleId int64) {
	defer serviceObj.roleBagInfo.Remove(roleId)
	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo != nil {
		bag_dao.NewOrUpdateBagInfo(bagInfo[pb.ProtocolManager_GetBagList],
			bagInfo[pb.ProtocolManager_GetCurrencyList], roleId)
	}
}

func onBagLoginEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	nRoleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	bagInfo := bag_dao.GetBagInfoByRoleId(nRoleId)
	items := make(map[int32]int32)
	currency := make(map[int32]int32)
	if bagInfo != nil {
		items = bagInfo.Items.Data()
		currency = bagInfo.Currency.Data()
	}

	defer serviceObj.setRoleBagInfo(nRoleId, items, currency)

	msg := &pb.GetBagListS2C{
		ItemMap: items,
	}
	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetBagList), msg)

	currencyMsg := &pb.GetCurrencyListS2C{
		CurrencyMap: currency,
	}
	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetCurrencyList), currencyMsg)
}

// 获取物品数量
func GetItemNum(roleId int64, itemId int32) int32 {
	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo == nil {
		return 0
	}
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	if currencyId, ok := sheetCurrencyMap[itemId]; ok {
		count, ok := bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId]
		if !ok {
			return 0
		}
		return count
	} else {
		count, ok := bagInfo[pb.ProtocolManager_GetBagList][itemId]
		if !ok {
			return 0
		}
		return count
	}
}

// 批量获取物品数量
func GetItemsNum(roleId int64, itemIds []int32) map[int32]int32 {
	resMap := make(map[int32]int32)

	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo == nil {
		return resMap
	}

	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()

	for _, itemId := range itemIds {
		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			count, ok := bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId]
			if !ok {
				resMap[currencyId] = 0
			}
			resMap[currencyId] = count
		} else {
			count, ok := bagInfo[pb.ProtocolManager_GetBagList][itemId]
			if !ok {
				resMap[itemId] = 0
			}
			resMap[itemId] = count
		}
	}

	return resMap
}

// 检查物品是否足够
func CheckItemEnough(roleId int64, itemId int32, needCount int32) bool {
	if needCount <= 0 {
		return true
	}
	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo == nil {
		return false
	}
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	if currencyId, ok := sheetCurrencyMap[itemId]; ok {
		count, ok := bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId]
		if !ok {
			return false
		}

		return count >= needCount
	} else {
		count, ok := bagInfo[pb.ProtocolManager_GetBagList][itemId]
		if !ok {
			return false
		}

		return count >= needCount
	}

}

func CheckBatchItemEnough(roleId int64, itemsMap map[int32]int32) bool {
	if len(itemsMap) <= 0 {
		return true
	}

	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo == nil {
		return false
	}
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	for itemId, needCount := range itemsMap {
		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			count, ok := bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId]
			if !ok {
				return false
			}

			if count < needCount {
				return false
			}
		} else {
			count, ok := bagInfo[pb.ProtocolManager_GetBagList][itemId]
			if !ok {
				return false
			}

			if count < needCount {
				return false
			}
		}
	}
	return true
}

func CheckArrayItemEnough(roleId int64, itemsArray [][]int32) bool {
	if len(itemsArray) <= 0 {
		return true
	}

	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo == nil {
		return false
	}
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	for _, array := range itemsArray {
		if len(array) != 2 {
			return false
		}

		itemId := array[0]
		needCount := array[1]
		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			count, ok := bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId]
			if !ok {
				return false
			}

			if count < needCount {
				return false
			}
		} else {
			count, ok := bagInfo[pb.ProtocolManager_GetBagList][itemId]
			if !ok {
				return false
			}

			if count < needCount {
				return false
			}
		}
	}
	return true
}

// 单次增加物品
func AddItemSingle(roleId int64, itemId int32, itemCount int32) {
	if itemId <= 0 || itemCount <= 0 {
		return
	}
	addItemMap := make(map[int32]int32)
	addItemMap[itemId] = itemCount
	AddItemBatch(roleId, addItemMap)
}

// 批量添加物品
func AddItemBatch(roleId int64, addItemMap map[int32]int32) {
	if len(addItemMap) <= 0 {
		return
	}

	bagInfo, _ := GetRoleBagInfo(roleId)
	bSet := false
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	sheetItemMap := config_manager.GetTableItemSheetItemMap()
	for itemId, count := range addItemMap {
		if count <= 0 || itemId <= 0 {
			continue
		}
		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			_, ok := bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId]
			if !ok {
				if bagInfo[pb.ProtocolManager_GetCurrencyList] == nil {
					if bagInfo == nil {
						bagInfo = make(map[pb.ProtocolManager]map[int32]int32)
						bSet = true
					}
					bagInfo[pb.ProtocolManager_GetCurrencyList] = make(map[int32]int32)
				}
				bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId] = count
			} else {
				bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId] += count
			}
		} else {
			if _, ok := sheetItemMap[itemId]; !ok {
				continue
			}
			_, ok := bagInfo[pb.ProtocolManager_GetBagList][itemId]
			if !ok {
				if bagInfo[pb.ProtocolManager_GetBagList] == nil {
					if bagInfo == nil {
						bagInfo = make(map[pb.ProtocolManager]map[int32]int32)
						bSet = true
					}
					bagInfo[pb.ProtocolManager_GetBagList] = make(map[int32]int32)
				}
				bagInfo[pb.ProtocolManager_GetBagList][itemId] = count
			} else {
				bagInfo[pb.ProtocolManager_GetBagList][itemId] += count
			}
		}
	}

	sendBagUpdateMsg(roleId, bagInfo[pb.ProtocolManager_GetBagList])
	sendCurrencyUpdateMsg(roleId, bagInfo[pb.ProtocolManager_GetCurrencyList])

	if bSet {
		serviceObj.setRoleBagInfo(roleId, bagInfo[pb.ProtocolManager_GetBagList],
			bagInfo[pb.ProtocolManager_GetCurrencyList])
	}

	//bag_dao.NewOrUpdateBagInfo(bagInfo[pb.ProtocolManager_GetBagList], bagInfo[pb.ProtocolManager_GetCurrencyList], roleId)
}

// 批量添加物品
func AddItemArrayInt32(roleId int64, addItemArray []*configpb.Array_INT32) {
	if len(addItemArray) <= 0 {
		return
	}

	for _, arr := range addItemArray {
		if len(arr.Value) != 2 {
			return
		}
	}

	bagInfo, _ := GetRoleBagInfo(roleId)
	bSet := false
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	sheetItemMap := config_manager.GetTableItemSheetItemMap()
	for _, array := range addItemArray {
		itemId := array.Value[0]
		count := array.Value[1]
		if count <= 0 || itemId <= 0 {
			continue
		}
		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			_, ok := bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId]
			if !ok {
				if bagInfo[pb.ProtocolManager_GetCurrencyList] == nil {
					if bagInfo == nil {
						bagInfo = make(map[pb.ProtocolManager]map[int32]int32)
						bSet = true
					}
					bagInfo[pb.ProtocolManager_GetCurrencyList] = make(map[int32]int32)
				}
				bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId] = count
			} else {
				bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId] += count
			}
		} else {
			if _, ok := sheetItemMap[itemId]; !ok {
				continue
			}
			_, ok := bagInfo[pb.ProtocolManager_GetBagList][itemId]
			if !ok {
				if bagInfo[pb.ProtocolManager_GetBagList] == nil {
					if bagInfo == nil {
						bagInfo = make(map[pb.ProtocolManager]map[int32]int32)
						bSet = true
					}
					bagInfo[pb.ProtocolManager_GetBagList] = make(map[int32]int32)
				}
				bagInfo[pb.ProtocolManager_GetBagList][itemId] = count
			} else {
				bagInfo[pb.ProtocolManager_GetBagList][itemId] += count
			}
		}
	}

	sendBagUpdateMsg(roleId, bagInfo[pb.ProtocolManager_GetBagList])
	sendCurrencyUpdateMsg(roleId, bagInfo[pb.ProtocolManager_GetCurrencyList])

	if bSet {
		serviceObj.setRoleBagInfo(roleId, bagInfo[pb.ProtocolManager_GetBagList],
			bagInfo[pb.ProtocolManager_GetCurrencyList])
	}

	//bag_dao.NewOrUpdateBagInfo(bagInfo[pb.ProtocolManager_GetBagList], bagInfo[pb.ProtocolManager_GetCurrencyList], roleId)
}

// 批量添加物品
func AddItemArray(roleId int64, addItemArray [][]int32) {
	if len(addItemArray) <= 0 {
		return
	}

	for _, arr := range addItemArray {
		if len(arr) != 2 {
			return
		}
	}

	bagInfo, _ := GetRoleBagInfo(roleId)
	bSet := false
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	sheetItemMap := config_manager.GetTableItemSheetItemMap()
	for _, array := range addItemArray {
		itemId := array[0]
		count := array[1]
		if count <= 0 || itemId <= 0 {
			continue
		}
		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			_, ok := bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId]
			if !ok {
				if bagInfo[pb.ProtocolManager_GetCurrencyList] == nil {
					if bagInfo == nil {
						bagInfo = make(map[pb.ProtocolManager]map[int32]int32)
						bSet = true
					}
					bagInfo[pb.ProtocolManager_GetCurrencyList] = make(map[int32]int32)
				}
				bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId] = count
			} else {
				bagInfo[pb.ProtocolManager_GetCurrencyList][currencyId] += count
			}
		} else {
			if _, ok := sheetItemMap[itemId]; !ok {
				continue
			}
			_, ok := bagInfo[pb.ProtocolManager_GetBagList][itemId]
			if !ok {
				if bagInfo[pb.ProtocolManager_GetBagList] == nil {
					if bagInfo == nil {
						bagInfo = make(map[pb.ProtocolManager]map[int32]int32)
						bSet = true
					}
					bagInfo[pb.ProtocolManager_GetBagList] = make(map[int32]int32)
				}
				bagInfo[pb.ProtocolManager_GetBagList][itemId] = count
			} else {
				bagInfo[pb.ProtocolManager_GetBagList][itemId] += count
			}
		}
	}

	sendBagUpdateMsg(roleId, bagInfo[pb.ProtocolManager_GetBagList])
	sendCurrencyUpdateMsg(roleId, bagInfo[pb.ProtocolManager_GetCurrencyList])

	if bSet {
		serviceObj.setRoleBagInfo(roleId, bagInfo[pb.ProtocolManager_GetBagList],
			bagInfo[pb.ProtocolManager_GetCurrencyList])
	}

	//bag_dao.NewOrUpdateBagInfo(bagInfo[pb.ProtocolManager_GetBagList], bagInfo[pb.ProtocolManager_GetCurrencyList], roleId)
}

// 单次减少物品
func SubItemSingle(roleId int64, itemId int32, subCount int32) (pb.ErrorCodeManager, bool) {
	if itemId <= 0 || subCount <= 0 {
		return pb.ErrorCodeManager_PARAM_ERROR, false
	}

	subItemMap := make(map[int32]int32)
	subItemMap[itemId] = subCount
	return SubItemBatch(roleId, subItemMap)
}

// 批量扣除物品，当物品ID非法或者物品数量不够扣除时，返回false
func SubItemBatch(roleId int64, subItemMap map[int32]int32) (pb.ErrorCodeManager, bool) {
	if len(subItemMap) <= 0 {
		return pb.ErrorCodeManager_SUCCESS, true
	}

	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo == nil {
		return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
	}
	itemMap := bagInfo[pb.ProtocolManager_GetBagList]
	currencyMap := bagInfo[pb.ProtocolManager_GetCurrencyList]
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	sheetItemMap := config_manager.GetTableItemSheetItemMap()

	retItemMap := make(map[int32]int32)
	retCurrencyMap := make(map[int32]int32)
	for itemId, count := range subItemMap {
		if count <= 0 {
			continue
		}

		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			_, ok := currencyMap[currencyId]
			if !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}

			if currencyMap[currencyId] < count {
				return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
			}
			if _, ok := retCurrencyMap[currencyId]; !ok {
				retCurrencyMap[currencyId] = count
			} else {
				retCurrencyMap[currencyId] += count
			}
		} else {
			if _, ok := sheetItemMap[itemId]; !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}
			_, ok := itemMap[itemId]
			if !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}

			if itemMap[itemId] < count {
				return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
			}

			if _, ok := retItemMap[itemId]; !ok {
				retItemMap[itemId] = count
			} else {
				retItemMap[itemId] += count
			}
		}

	}

	msgMap := make(map[int32]int32)
	msgCurrencyMap := make(map[int32]int32)
	if len(retItemMap) > 0 {
		for itemId, count := range retItemMap {
			itemMap[itemId] -= count
			if itemMap[itemId] < 0 {
				itemMap[itemId] = 0
			}
			msgMap[itemId] = itemMap[itemId]
		}
	}

	if len(retCurrencyMap) > 0 {
		for itemId, count := range retCurrencyMap {
			currencyMap[itemId] -= count
			if currencyMap[itemId] < 0 {
				currencyMap[itemId] = 0
			}
			msgCurrencyMap[itemId] = currencyMap[itemId]
		}
	}

	sendBagUpdateMsg(roleId, msgMap)
	sendCurrencyUpdateMsg(roleId, msgCurrencyMap)
	defer func() {
		conn := world_manager.WorldMgrObj.GetPlayerConn(roleId)
		event_manager.EventManagerObj.FireEventData(event_impl.NewItemUseEventEvent(conn, subItemMap))
	}()

	return pb.ErrorCodeManager_SUCCESS, true
}

// 批量扣除物品，当物品ID非法或者物品数量不够扣除时，返回false
func SubItemArray(roleId int64, subItemArray [][]int32) (pb.ErrorCodeManager, bool) {
	if len(subItemArray) <= 0 {
		return pb.ErrorCodeManager_SUCCESS, true
	}

	for _, arr := range subItemArray {
		if len(arr) != 2 {
			return pb.ErrorCodeManager_PARAM_ERROR, false
		}
	}

	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo == nil {
		return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
	}
	itemMap := bagInfo[pb.ProtocolManager_GetBagList]
	currencyMap := bagInfo[pb.ProtocolManager_GetCurrencyList]
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	sheetItemMap := config_manager.GetTableItemSheetItemMap()

	retItemMap := make(map[int32]int32)
	retCurrencyMap := make(map[int32]int32)
	for _, arr := range subItemArray {
		itemId := arr[0]
		count := arr[1]
		if count <= 0 {
			continue
		}

		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			_, ok := currencyMap[currencyId]
			if !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}

			if currencyMap[currencyId] < count {
				return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
			}
			if _, ok := retCurrencyMap[currencyId]; !ok {
				retCurrencyMap[currencyId] = count
			} else {
				retCurrencyMap[currencyId] += count
			}
		} else {
			if _, ok := sheetItemMap[itemId]; !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}
			_, ok := itemMap[itemId]
			if !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}

			if itemMap[itemId] < count {
				return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
			}

			if _, ok := retItemMap[itemId]; !ok {
				retItemMap[itemId] = count
			} else {
				retItemMap[itemId] += count
			}
		}

	}

	msgMap := make(map[int32]int32)
	msgCurrencyMap := make(map[int32]int32)
	if len(retItemMap) > 0 {
		for itemId, count := range retItemMap {
			itemMap[itemId] -= count
			if itemMap[itemId] < 0 {
				itemMap[itemId] = 0
			}
			msgMap[itemId] = itemMap[itemId]
		}
	}

	if len(retCurrencyMap) > 0 {
		for itemId, count := range retCurrencyMap {
			currencyMap[itemId] -= count
			if currencyMap[itemId] < 0 {
				currencyMap[itemId] = 0
			}
			msgCurrencyMap[itemId] = currencyMap[itemId]
		}
	}

	sendBagUpdateMsg(roleId, msgMap)
	sendCurrencyUpdateMsg(roleId, msgCurrencyMap)
	defer func() {
		conn := world_manager.WorldMgrObj.GetPlayerConn(roleId)
		subMaps := util.MergeMaps(retItemMap, retCurrencyMap)
		event_manager.EventManagerObj.FireEventData(event_impl.NewItemUseEventEvent(conn, subMaps))
	}()
	return pb.ErrorCodeManager_SUCCESS, true
}

// 批量扣除物品，当物品ID非法或者物品数量不够扣除时，返回false
func SubItemArrayInt32(roleId int64, subItemArray []*configpb.Array_INT32) (pb.ErrorCodeManager, bool) {
	if len(subItemArray) <= 0 {
		return pb.ErrorCodeManager_SUCCESS, true
	}

	for _, arr := range subItemArray {
		if len(arr.Value) != 2 {
			return pb.ErrorCodeManager_PARAM_ERROR, false
		}
	}

	bagInfo, _ := GetRoleBagInfo(roleId)
	if bagInfo == nil {
		return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
	}
	itemMap := bagInfo[pb.ProtocolManager_GetBagList]
	currencyMap := bagInfo[pb.ProtocolManager_GetCurrencyList]
	sheetCurrencyMap := config_manager.GetTableCurrencySheetCurrencyBYItemId()
	sheetItemMap := config_manager.GetTableItemSheetItemMap()

	retItemMap := make(map[int32]int32)
	retCurrencyMap := make(map[int32]int32)
	for _, arr := range subItemArray {
		itemId := arr.Value[0]
		count := arr.Value[1]
		if count <= 0 {
			continue
		}

		if currencyId, ok := sheetCurrencyMap[itemId]; ok {
			_, ok := currencyMap[currencyId]
			if !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}

			if currencyMap[currencyId] < count {
				return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
			}
			if _, ok := retCurrencyMap[currencyId]; !ok {
				retCurrencyMap[currencyId] = count
			} else {
				retCurrencyMap[currencyId] += count
			}
		} else {
			if _, ok := sheetItemMap[itemId]; !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}
			_, ok := itemMap[itemId]
			if !ok {
				return pb.ErrorCodeManager_PARAM_ERROR, false
			}

			if itemMap[itemId] < count {
				return pb.ErrorCodeManager_ITEM_NOT_ENOUGH, false
			}

			if _, ok := retItemMap[itemId]; !ok {
				retItemMap[itemId] = count
			} else {
				retItemMap[itemId] += count
			}
		}

	}

	msgMap := make(map[int32]int32)
	msgCurrencyMap := make(map[int32]int32)
	if len(retItemMap) > 0 {
		for itemId, count := range retItemMap {
			itemMap[itemId] -= count
			if itemMap[itemId] < 0 {
				itemMap[itemId] = 0
			}
			msgMap[itemId] = itemMap[itemId]
		}
	}

	if len(retCurrencyMap) > 0 {
		for itemId, count := range retCurrencyMap {
			currencyMap[itemId] -= count
			if currencyMap[itemId] < 0 {
				currencyMap[itemId] = 0
			}
			msgCurrencyMap[itemId] = currencyMap[itemId]
		}
	}

	sendBagUpdateMsg(roleId, msgMap)
	sendCurrencyUpdateMsg(roleId, msgCurrencyMap)
	defer func() {
		conn := world_manager.WorldMgrObj.GetPlayerConn(roleId)
		subMaps := util.MergeMaps(retItemMap, retCurrencyMap)
		event_manager.EventManagerObj.FireEventData(event_impl.NewItemUseEventEvent(conn, subMaps))
	}()
	return pb.ErrorCodeManager_SUCCESS, true
}

func sendBagUpdateMsg(roleId int64, itemMap map[int32]int32) {
	if len(itemMap) <= 0 {
		return
	}
	msg := &pb.BagUpdateS2C{
		ItemMap: itemMap,
	}

	world_manager.WorldMgrObj.PushClientMsg(roleId, pb.ProtocolManager_BagUpdate, msg)
}

func sendCurrencyUpdateMsg(roleId int64, currencyMap map[int32]int32) {
	if len(currencyMap) <= 0 {
		return
	}
	msg := &pb.CurrencyUpdateS2C{
		CurrencyMap: currencyMap,
	}

	world_manager.WorldMgrObj.PushClientMsg(roleId, pb.ProtocolManager_CurrencyUpdate, msg)
}

func (s *bagService) setRoleBagInfo(roleId int64, itemMap map[int32]int32, currencyMap map[int32]int32) {
	totalMap := make(map[pb.ProtocolManager]map[int32]int32)
	if len(itemMap) > 0 {
		totalMap[pb.ProtocolManager_GetBagList] = itemMap
	}
	if len(currencyMap) > 0 {
		totalMap[pb.ProtocolManager_GetCurrencyList] = currencyMap
	}

	if len(totalMap) > 0 {
		s.roleBagInfo.Set(roleId, totalMap)
	}
}

func GetRoleBagInfo(roleId int64) (map[pb.ProtocolManager]map[int32]int32, pb.ErrorCodeManager) {
	mapAny, ok := serviceObj.roleBagInfo.Get(roleId)
	if !ok {
		return nil, pb.ErrorCodeManager_ITEM_NOT_EXIST
	}

	bagMap := mapAny.(map[pb.ProtocolManager]map[int32]int32)
	return bagMap, pb.ErrorCodeManager_SUCCESS
}

func (s *bagService) GetModuleName() string {
	return "bag"
}

func (s *bagService) GetEventHandleFunc(eventType event_data.EventType) (func(event event_data.IEvent), bool) {
	eventFunc, ok := s.eventMethods[eventType]
	if !ok {
		zlog.Errorf("GetEventHandleFunc Err!!! method not found!! eventType: %d", eventType)
		return nil, false
	}
	return eventFunc, true
}

func (s *bagService) GetEventMap() map[event_data.EventType]func(event event_data.IEvent) {
	return s.eventMethods
}
